/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__ora_packet_check_fragment.c                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/21 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/oracle/config.h"
#include "../../test_ora_comm.h"
#include <oracle/ora_core.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <oracle/tns/ora_packet.h>

extern void __ora_destroy_ora_packet(L7_ora_packet **pp_st_ora_packet);
extern void __ora_destroy_session(L7session_ora **pp_st_session);
extern int __ora_packet_check_fragment(L7session_ora *p_st_sess
                                    , int i_is_request
                                    , char *p_payload
                                    , unsigned int ui_payload_len
                                    , struct timeval st_timestamp
                                    , L7_ora_packet *p_st_ora_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    L7_ora_packet *p_st_ora_packet = NULL;
    p_st_ora_packet = (L7_ora_packet *) frmalloc(sizeof(L7_ora_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_packet);
    memset(p_st_ora_packet, 0x00, sizeof(L7_ora_packet));

    CU_ASSERT_EQUAL(ora_init_ora_packet(p_st_ora_packet), FR_SUCC);

    char *p_bin = (char *) "000000d1062000000000035e010280290001013f01010d000004ffffffff010a047fffffff01010300000000000000000001000000000000000073656c656374206c6f632066726f6d206465707420776865726520646570746e6f3d3a312020616e6420646e616d653d3a322020616e64206c6f633d3a3320010100000000000001010002800000000002030000011600000000020369010001030000012800011000000203690100010300000120000110000002036901000702c10b0a4143434f554e54494e47084e455720594f524b";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(__ora_packet_check_fragment(p_st_ora_sess, FR_YES, arr_payload, strlen(p_bin)/2, st_timestamp, p_st_ora_packet), APM_ORA_CHECK_FRAGMENT_SUCCESS);

    __ora_destroy_ora_packet(&p_st_ora_packet);
    CU_ASSERT_PTR_NULL(p_st_ora_packet);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 正常测试
 */
static void __case2(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    L7_ora_packet *p_st_ora_packet = NULL;
    p_st_ora_packet = (L7_ora_packet *) frmalloc(sizeof(L7_ora_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_packet);
    memset(p_st_ora_packet, 0x00, sizeof(L7_ora_packet));

    CU_ASSERT_EQUAL(ora_init_ora_packet(p_st_ora_packet), FR_SUCC);

    char *p_bin = (char *) "000000d1062000000000035e010280290001013f01010d000004ffffffff010a047fffffff01010300000000000000000001000000000000000073656c656374206c6f632066726f6d206465707420776865726520646570746e6f3d3a312020616e6420646e616d653d3a322020616e64206c6f633d3a3320010100000000000001010002800000000002030000011600000000020369010001030000012800011000000203690100010300000120000110000002036901000702c10b0a4143434f554e54494e47084e455720594f52";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(__ora_packet_check_fragment(p_st_ora_sess, FR_YES, arr_payload, strlen(p_bin)/2, st_timestamp, p_st_ora_packet), APM_ORA_CHECK_FRAGMENT_NEED_NEXT_PACKET);

    __ora_destroy_ora_packet(&p_st_ora_packet);
    CU_ASSERT_PTR_NULL(p_st_ora_packet);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

/**
 * 异常测试
 */
static void __case3(void)
{
    L7session_ora *p_st_ora_sess = test_ora_session_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_sess);

    L7_ora_packet *p_st_ora_packet = NULL;
    p_st_ora_packet = (L7_ora_packet *) frmalloc(sizeof(L7_ora_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_ora_packet);
    memset(p_st_ora_packet, 0x00, sizeof(L7_ora_packet));

    CU_ASSERT_EQUAL(ora_init_ora_packet(p_st_ora_packet), FR_SUCC);

    char *p_bin = (char *) "000000d1062000000000035e010280290001013f01010d000004ffffffff010a047fffffff01010300000000000000000001000000000000000073656c656374206c6f632066726f6d206465707420776865726520646570746e6f3d3a312020616e6420646e616d653d3a322020616e64206c6f633d3a3320010100000000000001010002800000000002030000011600000000020369010001030000012800011000000203690100010300000120000110000002036901000702c10b0a4143434f554e54494e47084e455720594f524b00";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(__ora_packet_check_fragment(p_st_ora_sess, FR_YES, arr_payload, strlen(p_bin)/2, st_timestamp, p_st_ora_packet), APM_ORA_CHECK_FRAGMENT_ERROR);

    __ora_destroy_ora_packet(&p_st_ora_packet);
    CU_ASSERT_PTR_NULL(p_st_ora_packet);

    __ora_destroy_session(&p_st_ora_sess);
    CU_ASSERT_PTR_NULL(p_st_ora_sess);
}

int add_suite__ora_packet_check_fragment(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __ora_packet_check_fragment()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
